<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../assets/bootstrap/bootstrap.css">
    <style>
        .box {
            width: 600px;
            margin: 0 auto;
        }
        .list-group-item{
            display: flex;
            flex-flow: row nowrap;
        }
        .list-group-item>input{
            width: 16px;
            margin: 0px 10px;
        }
        .list-group-item>.desc{
            flex-grow: 1;
        }
        .list-group-item>.badge{
            margin: 0px 10px;
            line-height: 1.6;
            cursor: pointer;
        }
        .list-group-item.tip .desc{
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">

        <div class="box mt-4">
            <div class="card">
                <div class="card-body bg-light">
                    <h5>添加新邮箱{{ checkValidation() }}</h5>
                    <div>
                        <!-- is-invalid  is-valid -->
                        <input type="text" class="form-control" placeholder="新邮箱" 
                            :class=" checkValidation() "
                            @input=" email=$event.target.value "
                        
                        >
                    </div>
                    <div class="d-grid gap-2 mt-2">
                        <button class="btn btn-primary" type="button" @click="addItem()">添加</button>
                    </div>
                </div>
            </div>
            <div class="card mt-3">
                <div class="card-body bg-light">
                    <ul class="list-group"  v-show="emails.length==0">
                        <li class="list-group-item tip">
                            <span class="desc">请先添加邮箱</span>
                        </li>
                    </ul>
                    <ul class="list-group"  v-if="emails.length!=0">
                        <li class="list-group-item" v-for=" (e,i) in emails ">
                            <input type="checkbox" >
                            <span class="desc">{{ e }}</span>
                            <span class="badge rounded-pill bg-danger" @click="removeItem(i)">删除</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

    </div>
    <script type="module">
        import { createApp } from "../assets/vue3/vue.esm-browser.js";
        const app = createApp({
            data() {
                return {
                    email:"",
                    emails:[]
                }
            },
            methods:{
                addItem(){
                    this.emails.push( this.email );
                    this.email = "";
                },
                removeItem(i){
                    this.emails.splice(i,1)
                },
                checkValidation(){
                    if(this.email==""){
                        return "";
                    }
                    let reg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                    let flag = reg.test(this.email)
                    return flag?'is-valid':'is-invalid';
                }
            }
        })
        const vm = app.mount("#app")
        console.log(vm)
    </script>
</body>

</html>